home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / OpenGL / atlantis / atlantis.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.1 KB  |  364 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <math.h>
  40. #include <GL/gl.h>
  41. #include "tk.h"
  42. #include "atlantis.h"
  43.  
  44.  
  45. fishRec sharks[NUM_SHARKS];
  46. fishRec momWhale;
  47. fishRec babyWhale;
  48. fishRec dolph;
  49.  
  50. GLint windW, windH;
  51. GLenum directRender;
  52. GLenum mouseLeftAction = GL_FALSE, mouseRightAction = GL_FALSE,
  53.        mouseMiddleAction = GL_FALSE;
  54. char *fileName = 0;
  55. TK_RGBImageRec *image;
  56.  
  57.  
  58. void InitFishs(void)
  59. {
  60.     int i;
  61.  
  62.     for (i = 0; i < NUM_SHARKS; i++) {
  63.         sharks[i].x = 70000.0 + rand() % 6000;
  64.         sharks[i].y = rand() % 6000;
  65.         sharks[i].z = rand() % 6000;
  66.         sharks[i].psi = rand() % 360 - 180.0;
  67.         sharks[i].v = 1.0;
  68.     }
  69.  
  70.     dolph.x = 30000.0;
  71.     dolph.y = 0.0;
  72.     dolph.z = 6000.0;
  73.     dolph.psi = 90.0;
  74.     dolph.theta = 0.0;
  75.     dolph.v = 3.0;
  76.  
  77.     momWhale.x = 70000.0;
  78.     momWhale.y = 0.0;
  79.     momWhale.z = 0.0;
  80.     momWhale.psi = 90.0;
  81.     momWhale.theta = 0.0;
  82.     momWhale.v = 3.0;
  83.  
  84.     babyWhale.x = 60000.0;
  85.     babyWhale.y = -2000.0;
  86.     babyWhale.z = -2000.0;
  87.     babyWhale.psi = 90.0;
  88.     babyWhale.theta = 0.0;
  89.     babyWhale.v = 3.0;
  90. }
  91.  
  92. void Init(void)
  93. {
  94.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  95.     static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  96.     static float position[] = {0.0, 1.0, 0.0, 0.0};
  97.     static float mat_shininess[] = {90.0};
  98.     static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
  99.     static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
  100.     static float mat_ambient[] = {0.0, 0.1, 0.2, 1.0};
  101.     static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
  102.     static float lmodel_localviewer[] = {0.0};
  103.     GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
  104.     GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
  105.     
  106.     glFrontFace(GL_CW);
  107.  
  108.     glDepthFunc(GL_LEQUAL);
  109.     glEnable(GL_DEPTH_TEST);
  110.  
  111.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  112.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  113.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  114.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  115.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
  116.     glEnable(GL_LIGHTING);
  117.     glEnable(GL_LIGHT0);
  118.  
  119.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  120.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  121.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  122.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  123.  
  124.     if (fileName != 0) {
  125.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  126.     image = tkRGBImageLoad(fileName);
  127.  
  128.     glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0, GL_RGB,
  129.              GL_UNSIGNED_BYTE, (unsigned char *)image->data);
  130.  
  131.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  132.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  133.     glPushMatrix();
  134.     glMatrixMode(GL_MODELVIEW);
  135.     glLoadIdentity();
  136.     map1[0] = 1.0 / 200000.0 * 50.0;
  137.     map2[2] = 1.0 / 200000.0 * 50.0;
  138.     glTexGenfv(GL_S, GL_EYE_PLANE, map1);
  139.     glTexGenfv(GL_T, GL_EYE_PLANE, map2);
  140.     glPopMatrix();
  141.  
  142.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  143.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  144.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  145.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  146.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  147.  
  148.     glEnable(GL_TEXTURE_2D);
  149.     glEnable(GL_TEXTURE_GEN_S);
  150.     glEnable(GL_TEXTURE_GEN_T);
  151.     }
  152.  
  153.     InitFishs();
  154.  
  155.     glClearColor(0.0, 0.5, 0.9, 0.0);
  156. }
  157.  
  158. void Reshape(int width, int height)
  159. {
  160.  
  161.     windW = (GLint)width;
  162.     windH = (GLint)height;
  163.  
  164.     glViewport(0, 0, windW, windH);
  165.  
  166.     glMatrixMode(GL_PROJECTION);
  167.     glLoadIdentity();
  168.     gluPerspective(400.0, 1.0, 1.0, 2000000.0);
  169.     glMatrixMode(GL_MODELVIEW);
  170. }
  171.  
  172. void Animate(void);
  173.  
  174. GLenum Key(int key, GLenum mask)
  175. {
  176.  
  177.     switch (key) {
  178.       case TK_ESCAPE:
  179.     tkQuit();
  180.       case TK_r:
  181.       case TK_R:
  182.         InitFishs();
  183.         glClearColor(0.0, 0.5, 0.9, 0.0);
  184.         glLoadIdentity();
  185.         Reshape(windW,windH);
  186.         tkIdleFunc(Animate);
  187.       default:
  188.     return GL_FALSE;
  189.     }
  190.     return GL_TRUE;
  191. }
  192.  
  193. void DoMouseLeft(void)
  194. {
  195.     float x, y;
  196.     int mouseX, mouseY;
  197.     
  198.     tkGetMouseLoc(&mouseX, &mouseY);
  199.     x = (float)mouseX - ((float)windW / 2.0);
  200.     y = (float)mouseY - ((float)windH / 2.0);
  201.     glTranslatef(-x*20.0, y*20.0, 0.0);
  202. }
  203.  
  204. void DoMouseRight(void)
  205. {
  206.     float y;
  207.     int mouseX, mouseY;
  208.     
  209.     tkGetMouseLoc(&mouseX, &mouseY);
  210.     y = (float)mouseY - ((float)windH / 2.0);
  211.     glTranslatef(0.0, 0.0, -y*20.0);
  212. }
  213.  
  214. void DoMouseMiddle(void)
  215. {
  216.     float x, y;
  217.     int mouseX, mouseY;
  218.     
  219.     tkGetMouseLoc(&mouseX, &mouseY);
  220.     x = (float)mouseX - ((float)windW / 2.0);
  221.     y = (float)mouseY - ((float)windH / 2.0);
  222.     glRotatef(-x/100.0, 0.0, 1.0, 0.0);
  223.     glRotatef(-y/100.0, 1.0, 0.0, 0.0);
  224. }
  225.  
  226. GLenum MouseDown(int mouseX, int mouseY, GLenum button)
  227. {
  228.     float x, y;
  229.  
  230.     if (button & TK_LEFTBUTTON) {
  231.     mouseLeftAction = GL_TRUE;
  232.     }
  233.     if (button & TK_RIGHTBUTTON) {
  234.     mouseRightAction = GL_TRUE;
  235.     }
  236.     if (button & TK_MIDDLEBUTTON) {
  237.     mouseMiddleAction = GL_TRUE;
  238.     }
  239.     return GL_TRUE;
  240. }
  241.  
  242. GLenum MouseUp(int mouseX, int mouseY, GLenum button)
  243. {
  244.     float x, y;
  245.  
  246.     if (button & TK_LEFTBUTTON) {
  247.     mouseLeftAction = GL_FALSE;
  248.     }
  249.     if (button & TK_RIGHTBUTTON) {
  250.     mouseRightAction = GL_FALSE;
  251.     }
  252.     if (button & TK_MIDDLEBUTTON) {
  253.     mouseMiddleAction = GL_FALSE;
  254.     }
  255.     return GL_TRUE;
  256. }
  257.  
  258. void Animate(void)
  259. {
  260.     int i;
  261.  
  262.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  263.  
  264.     if (mouseLeftAction == GL_TRUE) {
  265.     DoMouseLeft();
  266.     }
  267.     if (mouseRightAction == GL_TRUE) {
  268.     DoMouseRight();
  269.     }
  270.     if (mouseMiddleAction == GL_TRUE) {
  271.     DoMouseMiddle();
  272.     }
  273.  
  274.     for (i = 0; i < NUM_SHARKS; i++) {
  275.     glPushMatrix();
  276.     SharkPilot(&sharks[i]);
  277.     SharkMiss(i);
  278.     FishTransform(&sharks[i]);
  279.     DrawShark(&sharks[i]);
  280.     glPopMatrix();
  281.     }
  282.  
  283.     glPushMatrix();
  284.     WhalePilot(&dolph);
  285.     dolph.phi++;
  286.     FishTransform(&dolph);
  287.     DrawDolphin(&dolph);
  288.     glPopMatrix();
  289.  
  290.     glPushMatrix();
  291.     WhalePilot(&momWhale);
  292.     momWhale.phi++;
  293.     FishTransform(&momWhale);
  294.     DrawWhale(&momWhale);
  295.     glPopMatrix();
  296.  
  297.     glPushMatrix();
  298.     WhalePilot(&babyWhale);
  299.     babyWhale.phi++;
  300.     FishTransform(&babyWhale);
  301.     glScalef(0.45, 0.45, 0.3);
  302.     DrawWhale(&babyWhale);
  303.     glPopMatrix();
  304.  
  305.     tkSwapBuffers();
  306. }
  307.  
  308. GLenum Args(int argc, char **argv)
  309. {
  310.     GLint i;
  311.  
  312.     directRender = GL_TRUE;
  313.  
  314.     for (i = 1; i < argc; i++) {
  315.     if (strcmp(argv[i], "-dr") == 0) {
  316.         directRender = GL_TRUE;
  317.     } else if (strcmp(argv[i], "-ir") == 0) {
  318.         directRender = GL_FALSE;
  319.     } else if (strcmp(argv[i], "-f") == 0) {
  320.         if (i+1 >= argc || argv[i+1][0] == '-') {
  321.         printf("-f (No file name).\n");
  322.         return GL_FALSE;
  323.         } else {
  324.         fileName = argv[++i];
  325.         }
  326.     } else {
  327.         printf("%s (Bad option).\n", argv[i]);
  328.         return GL_FALSE;
  329.     }
  330.     }
  331.     return GL_TRUE;
  332. }
  333.  
  334. void main(int argc, char **argv)
  335. {
  336.     GLenum type;
  337.  
  338.     if (Args(argc, argv) == GL_FALSE) {
  339.     tkQuit();
  340.     }
  341.  
  342.     windW = 600;
  343.     windH = 600;
  344.     tkInitPosition(0, 0, windW, windH);
  345.  
  346.     type = TK_RGB | TK_DOUBLE | TK_DEPTH;
  347.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  348.     tkInitDisplayMode(type);
  349.  
  350.     if (tkInitWindow("Atlantis Demo") == GL_FALSE) {
  351.     tkQuit();
  352.     }
  353.  
  354.     Init();
  355.  
  356.     tkExposeFunc(Reshape);
  357.     tkReshapeFunc(Reshape);
  358.     tkKeyDownFunc(Key);
  359.     tkMouseDownFunc(MouseDown);
  360.     tkMouseUpFunc(MouseUp);
  361.     tkIdleFunc(Animate);
  362.     tkExec();
  363. }
  364.